//
//  HJInformationBlockchainCell.m
//  KaiChat
//
//  Created by lx on 2024/3/11.
//

#import "HJInformationBlockchainCell.h"
#import "HJInformationOperateView.h"
#import "HJImageView.h"

@interface HJInformationBlockchainCell()


@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UIImageView *contentImageView;
@property (nonatomic, weak) UILabel *sourceLabel;
@property (nonatomic, weak) HJInformationOperateView *operateView;

@end

@implementation HJInformationBlockchainCell



- (void)settingSubView {
    [super settingSubView];

    
    self.selectionStyle = UITableViewCellSelectionStyleNone;

    UILabel *titleLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMediumMake(14) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
    titleLabel.numberOfLines = 3;
 
    HJImageView *contentImageView = [HJImageView addToView:self.contentView];
    contentImageView.clipsToBounds = YES;
    contentImageView.contentMode = UIViewContentModeScaleAspectFill;
    contentImageView.addCornerRadius(4);
    
    UILabel *sourceLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.tj_BEBDCAColor title:nil textAlignment:NSTextAlignmentLeft];
    
    UILabel *timeLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.tj_BEBDCAColor title:nil textAlignment:NSTextAlignmentLeft];
    
    HJInformationOperateView *operateView = [[HJInformationOperateView alloc] initWithTypes:@[
        @(HJInformationOperateLike),
        @(HJInformationOperateShare),
        @(HJInformationOperateComment),
        @(HJInformationOperateViews),
    ]];
    
    TJWeakify(self)
    [operateView setDidClickButtonBlock:^(UIButton * _Nonnull button, HJInformationOperateType type) {
        if ([weakself.delegate respondsToSelector:@selector(informationCellDidClickOperateViewButton:type:button:)]) {
            [weakself.delegate informationCellDidClickOperateViewButton:weakself type:type button:button];
        }
    }];
    
    [self.contentView addSubview:operateView];
    
    UIView *lineView = [UIView addToView:self.contentView];
    lineView.backgroundColor = UIColor.tj_separatorColor;
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.height.offset(0.7);
        make.bottom.offset(0);
    }];

    _timeLabel = timeLabel;
    _titleLabel = titleLabel;
    _contentImageView = contentImageView;
    _sourceLabel = sourceLabel;
    _operateView = operateView;
}

 
- (void)layoutSubviews {
    [super layoutSubviews];
    
    _timeLabel.frame = self.layout.timeRect;
    _titleLabel.frame = self.layout.titleRect;
    _sourceLabel.frame = self.layout.sourceRect;
    _operateView.frame = self.layout.operateRect;
    _contentImageView.frame = self.layout.imageRect;
}

- (void)setLayout:(HJBlockchainInformationLayout *)layout {
    _layout = layout;
    
    _timeLabel.text = layout.model.publishTime;
    _titleLabel.attributedText = layout.attributedString;
    _titleLabel.lineBreakMode = NSLineBreakByTruncatingTail;

    if (layout.model.images.count) {
        _contentImageView.hidden = NO;
        [_contentImageView tj_setImageWithUrl:layout.model.images.firstObject placeholderImage:[UIImage imageNamed:@"bigLoad"]];
    } else {
        _contentImageView.hidden = YES;
        _contentImageView.image = nil;
    }
 
    _operateView.likeButton.selected = layout.model.is_like;
    _sourceLabel.text = [NSString stringWithFormat:@"%@", layout.model.source];
    _operateView.likeButton.selected = layout.model.is_like;
    [_operateView.likeButton setTitle:[NSString stringWithFormat:@"%ld", layout.model.like] forState:UIControlStateNormal];
    [_operateView.shareButton setTitle:[NSString stringWithFormat:@"%ld", layout.model.share] forState:UIControlStateNormal];
    [_operateView.viewsButton setTitle:[NSString stringWithFormat:@"%ld", layout.model.views] forState:UIControlStateNormal];
    [_operateView.commentButton setTitle:[NSString stringWithFormat:@"%ld", layout.model.comment] forState:UIControlStateNormal];
    
}

 


@end
